package com.gs.study.web;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gs.study.util.BaseController;
import com.gs.study.util.ResultJson;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
 * restfull风格
 * @author gs
 *
 */
@Controller
@RequestMapping(value="/hel")
public class HelloController extends BaseController{
    @Resource
    Configuration cfg;
	private Logger logger = LoggerFactory.getLogger(getClass());

    
	/**
	 * 获取数据
	 * @return
	 */
    @RequestMapping(value="/getUser",method=RequestMethod.GET) 
    @ResponseBody
    public Map<String,Object> getUser(){  
		logger.debug("Logger Level ：DEBUG");
		logger.info("Logger Level ：INFO");
		logger.error("Logger Level ：ERROR");
        Map<String,Object> map = new HashMap<>();     
        map.put("Name", "高尚");  
        map.put("Date", new Date());  
        return map;  
    } 
    
    /**
     * 提交数据
     * @return
     */
    @RequestMapping(value="/postUser",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> postUser(){  
        Map<String,Object> map = new HashMap<>();     
        map.put("Name", "高尚");  
        map.put("Date", new Date());  
        return map;  
    } 
    /**
     * 更新数据
     * @return
     */
    @RequestMapping(value="/putUser",method=RequestMethod.PUT)
    @ResponseBody
    public Map<String,Object> putUser(){  
        Map<String,Object> map = new HashMap<>();     
        map.put("Name", "高尚");  
        map.put("Date", new Date());  
        return map;  
    } 
    /**
     * 删除
     * @return
     */
    @RequestMapping(value="/deleteUser",method=RequestMethod.DELETE)
    @ResponseBody
    public Map<String,Object> deleteUser(){  
          
        Map<String,Object> map = new HashMap<>();     
        map.put("Name", "高尚");  
        map.put("Date", new Date());  
        return map;  
    } 

    /**
     * thymeleaf模板渲染web视图
     * @param map
     * @return
     */
    @RequestMapping(value="/thymeleaf")
    public String thymeleaf(ModelMap map) {
        map.addAttribute("host", "http://blog.didispace.com");
        return "thymeleaf";
    }
    
    /**
     * freemarker模板渲染web视图
     * @param map
     * @return
     */
    @RequestMapping(value="/freemarker")
    public String freemarker(ModelMap map) {
        map.addAttribute("host", "http://blog.didispace.com");
        return "freemarker";
    }
    
    
    /**
     * 生成HTML
     * @param root
     */
    @RequestMapping(value="/freeMarkerContent")
    private void freeMarkerContent(Map<String,Object> root){
    	ResultJson json=new ResultJson(true);
        try {
            Template temp = cfg.getTemplate("freemarker.ftl");
            //以classpath下面的static目录作为静态页面的存储目录，同时命名生成的静态html文件名称
            String path=this.getClass().getResource("/").toURI().getPath()+"test.html";
            Writer file = new FileWriter(new File(path.substring(path.indexOf("/"))));
            root.put("host", "http://blog.didispace.com");
            temp.process(root, file);
            file.flush();
            file.close();
            printString(json.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * velocity模板渲染web视图
     * @param map
     * @return
     */
    @RequestMapping(value="/velocity")
    public String velocity(ModelMap map) {
        map.addAttribute("host", "http://blog.didispace.com");
        return "velocity";
    }
    
    @RequestMapping("")
    public String index() {
        return "index";
    }
 
    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }
    
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }
    
    public static void main(String[] args) {
    	String str="<div class='p-row'>"
    +"<h1 class='p-title'>方案详情<span class='p-mark'>不要删除次行代码</span></h1>"
    +"<div class='p-detail'>方案详情页签代码</div>"
+"</div>";
    	//String newStr = str.replaceAll("<span class='p-mark'>不要删除次行代码</span>", "").replaceAll("<.*?>", "");
    	String newStr = "<div class='p-detail'>.*?</div>";

    
        Pattern pattern = Pattern.compile(newStr);// 匹配的模式
        Matcher m = pattern.matcher(str);
    	   while(m.find()){
    	    	System.out.println( m.group());
           }
	}
    
    
    
    /**
     * view:(跳转到JSP界面).  
     * @author Joe
     * Date:2017年11月6日下午4:29:27
     *
     * @param map
     * @return
     */
    @RequestMapping(value = {"/jsp", "/view"})
    public String view(Map<String, Object> map) {
        map.put("name", "SpringBoot");
        map.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return "jsp";
    }
}
